package com.example.my_bookkeeping;

import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;

import com.example.my_bookkeeping.data.manager.CategoryManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.*;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.matcher.ViewMatchers;
import org.hamcrest.Matcher;
import android.view.View;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class EditCategoryTest {

    private CategoryManager categoryManager;

    @Rule
    public ActivityScenarioRule<MainActivity> activityScenarioRule = new ActivityScenarioRule<>(MainActivity.class);

    @Before
    public void setup() {
        categoryManager = CategoryManager.getInstance();
        categoryManager.reset();
    }

    @After
    public void teardown() {
        // 清理所有数据
        categoryManager.reset();
    }

    @Test
    public void testEditCategory() throws InterruptedException {
        Thread.sleep(1000);
        // 1. 导航到分类管理页面
        onView(allOf(withText("账目分类"), isDisplayed())).perform(click());
        Thread.sleep(500);

        // 2. 找到一个现有的支出分类（例如"出行"）并点击编辑按钮
        onView(withId(R.id.recyclerView_category))
                .perform(RecyclerViewActions.actionOnItem(
                        hasDescendant(withText("出行")),
                        clickChildViewWithId(R.id.btn_edit)
                ));
        Thread.sleep(500);

        // 3. 在弹出的编辑对话框中：
        //    输入新的分类名称（例如"交通"）
        onView(withId(R.id.et_category_name)).perform(replaceText("交通"));
        //    选择"收入"Radio Button，将其类型修改为收入
        onView(withId(R.id.rb_income)).perform(click());
        Thread.sleep(500);

        //    点击"确定"按钮
        onView(withId(android.R.id.button1)).perform(click()); // android.R.id.button1 是 PositiveButton
        Thread.sleep(500);

        // 4. 验证：
        //    检查"出行"是否已从支出列表中移除
        onView(allOf(withText("出行"), withId(R.id.tv_category))).check(doesNotExist());

        //    切换到收入选项卡
        onView(allOf(withText("收入"), isDisplayed())).perform(click());
        Thread.sleep(500);

        //    检查"交通"是否已添加到收入列表中
        onView(allOf(withId(R.id.recyclerView_category), isDisplayed()))
                .check(matches(hasDescendant(withText("交通"))));
    }

    public static ViewAction clickChildViewWithId(final int id) {
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return allOf(ViewMatchers.isDisplayingAtLeast(90));
            }

            @Override
            public String getDescription() {
                return "Click on a child view with specified ID.";
            }

            @Override
            public void perform(UiController uiController, View view) {
                View v = view.findViewById(id);
                if (v != null) {
                    v.performClick();
                } else {
                    throw new RuntimeException("Child view with id " + id + " not found.");
                }
            }
        };
    }
} 